package com.mate.cloud.sensitive.processor;

import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.security.KeyPair;

@SpringBootTest(classes = RSAProcessorTest.class)
public class RSAProcessorTest {


    @Test
    public void base64EncoderTest() {
        KeyPair pair = SecureUtil.generateKeyPair("RSA");
        byte[] encoded = pair.getPrivate().getEncoded();
        System.out.println("encoded:" + encoded);
        String encode = Base64Encoder.encode(encoded);
        System.out.println("encode:" + encode);
        byte[] encoded1 = pair.getPublic().getEncoded();
        System.out.println("encoded1:" + encoded1);
        String encode1 = Base64Encoder.encode(encoded1);
        System.out.println("encode1:" + encode1);
        String text = "测试加密数据";
        RSAProcessor converter = new RSAProcessor(encode1, encode);
        byte[] encrypt = converter.encrypt(StrUtil.bytes(text, CharsetUtil.CHARSET_UTF_8));
        String encodeHexStr = HexUtil.encodeHexStr(encrypt, false);
        System.out.println("encodeHexStr:" + encodeHexStr);
        byte[] decrypt = converter.decrypt(encodeHexStr);
        System.out.println("decrypt:" + StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
    }
}
